<?php

class Action
{
    private $per_page = 10;

    public function index(){
        $page_no  = $search_filter = $is_excel = null;

        extract(URLTracker::init(array(
            'page_no' => 1,
            'search_filter' => array(),
            'is_excel' => 0,
        )));
        $relation = array();

        $relation['date !='] = "";
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $relation['date >='] = $search_filter['start_date'];
        }

        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $relation['date <='] = $search_filter['end_date'];
        }


        $relation_count = _model('admin_app_order_device_bulletin_day')->getTotal($relation);
        if($relation_count){
            $pager = new Pager($this->per_page);
            if ($pager->generate($relation_count)) {
                Response::assign('pager', $pager);
            }
            $device_by = ' ORDER BY `id` DESC ';
            if ($is_excel != 1) {
                $device_by .= $pager->getLimit($page_no);
            }

            $relation_info = _model('admin_app_order_device_bulletin_day')->getList($relation, $device_by);
            foreach($relation_info as $k => $v){
                $relation_info[$k]['device_bind_data'] = json_decode($v['device_bind_data'], true);
            }
            if ($is_excel == 1) {
                $this->export_excel($relation_info);
            }
            Response::assign('count', $relation_count);
        }

        if (empty($relation_info)) {
            $relation_info = array();
        }
        Response::assign('relation_info', $relation_info);
        Response::assign('search_filter', $search_filter);

        Response::display('admin/device_bulletin.html');
    }

    /**
     * 绑定设备简报信息导出
     */
    public function export_excel($list = array()) {
        $lines = array();
        foreach ($list as $k => $v) {
            array_push($lines, array(
                $v['device_bind_data']['zx_machine_num'],
                $v['device_bind_data']['mx_machine_num'],
                $v['device_bind_data']['fx_machine_num'],
                $v['device_bind_data']['os_machine_num'],
                $v['device_bind_data']['all_machine_num'],
                $v['date'],
            ));
        }
        //初始化excel对象
        $objPHPExcel = new PHPExcel();
        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        //设置当前的sheet索引，用于后续的内容操纵
        $objPHPExcel->setActiveSheetIndex(0);
        $objActSheet = $objPHPExcel->getActiveSheet();
        // 哪几列
        $col_arr = array('A', 'B', 'C', 'D', 'E', 'F');
        // 第一排的标题
        $col_title_arr = array('ZX设备', 'MX设备', 'FX设备', 'OS设备', '合计数', '日期');

        foreach ($col_arr as $k => $v) {
            //设置单位格内容A1,B1,C1,D1
            $objActSheet->setCellValue($v . "1", $col_title_arr[$k]);
        }
        //填充单元格
        $key = 2;
        foreach ($lines as $k => $v) {
            foreach ($col_arr as $k2 => $v2) {
                $place = $v2 . $key;
                $objActSheet->setCellValue($place, $lines[$k][$k2]);
            }
            $key++;
        }

        //设置页面类型
        header("Content-Type: application/vnd.ms-excel");
        //用于下载的文件头
        header("Content-Type: application/force-download");
        header("Content-Type: application/download");
        header("Content-Type: application/octet-stream");

        //$title = trim($catebirthday_gift_recordry_name."表").".xls";
        $title = '截至'.date("Y-m-d")."绑定设备简报统计.xls";
        // ff正常，ie不正常，下面是从网站找到的解决方案
        if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) {
            $title = rawurlencode($title);
        }
        header("Content-Disposition: attachment; filename=" . $title);
        header("Content-Transfer-Encoding: binary");
        //最后修改时间
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        //不使用缓存
        header('Cache-Control: ust-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: public');
        header("Expires: 0");
        header("Pragma: public");
        $objWriter->save('php://output');
        exit;
    }
}